package cn.ccew.apk.web_manage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONSerializer;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.ccew.apk.model.Software;
import cn.ccew.sys.web.SysBaseController;

@Controller
public class ManageController extends SysBaseController {
	/**
	 * 加载左则树列表
	 * 
	 * @param response
	 * @param pid
	 * @throws Exception
	 */
	@RequestMapping("software_loadList.wb")
	public void software_loadList(HttpServletResponse response, String pid) throws Exception {

		Criteria criteria = this.createCriteria(Software.class);

		criteria.addOrder(Order.desc("priority"));
		// 关联查询时使用Distinct方法去除重复数据
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

		List<Software> list = criteria.list();
		List<Map<Object, Object>> mapList = new ArrayList<Map<Object, Object>>();

		for (Software temp : list) {
			mapList.add(getMapObj(temp));
		}

		writerTo(response, JSONSerializer.toJSON(mapList).toString());
	}

	/**
	 * 最大排序下标
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("software_MaxPriority.wb")
	public void software_MaxPriority(HttpServletRequest request, HttpServletResponse response, String id) throws Exception {
		List<Object> countList = null;
		countList = this.baseService.findBySql("select MAX(t.priority) from apk_software as t ");

		int maxValue = 0;
		if (null != countList && countList.size() > 0) {
			try {
				maxValue = Integer.parseInt(countList.get(0).toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Map<Object, Object> map = new HashMap<Object, Object>();

		map.put("priority", maxValue + 1);
		responseListJSON(response, map);
	}

	/**
	 * 加载一条记录，用于显示修改
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("software_load.wb")
	public void software_load(String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Software software = baseService.findById(id, Software.class);
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("id", software.getId());
		map.put("text", software.getText());
		map.put("leaf", software.getLeaf());
		map.put("priority", software.getPriority());
		responseListJSON(response, map);
	}

	/**
	 * 保存
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("software_save.wb")
	public void software_save(@Valid Software software, String pid, HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			baseService.saveOrUpdate(software);
			writerSuccess(response, "保存成功");
		} catch (Exception e) {
			writerFailure(response, "保存失败，未知原因");
			e.printStackTrace();
		}

	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("software_delete.wb")
	public void software_delete(String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Software root = baseService.findById(id, Software.class);
		try {
			if (root.getStatus() == 1) {
				writerFailure(response, "删除失败，系统版本不能删除");
				return;
			}
			baseService.delete(root.getId(), Software.class);
			writerSuccess(response, "删除成功");
		} catch (Exception e) {
			writerFailure(response, "删除失败，未知原因");
			e.printStackTrace();
		}

	}

	/**
	 * 把资源 对象封装成菜单对象
	 * 
	 * @param source
	 * @return
	 */
	private static Map<Object, Object> getMapObj(Software software) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("id", software.getId());
		map.put("text", software.getText());
		map.put("leaf", software.getLeaf());
		return map;
	}
}
